<template>
    <div class='account'>
        <div class="container">
            <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="search-con">
                <el-form-item label="部门名称" prop="key">
                    <el-input v-model="queryParams.key" placeholder="请输入" clearable size="small" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form> -->
            <el-row :gutter="10" class="mb8">
                <el-col :span="22">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="title='新增';handleShow='add'" v-hasPermi="['user:depart:add']">
                        新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight :height="tableList.length>0?'900px':'120px'">
                <el-table-column label="账户编号" align="center" prop="id" />
                <el-table-column label="账户名称" align="center" prop="name" />
                <el-table-column label="开户地" align="center" prop="open_location" />
                <el-table-column label="开户支行" align="center" prop="open_bank" />
                <el-table-column label="预留手机" align="center" prop="mobile" />
                <el-table-column label="银行卡号" align="center" prop="card_number" >
                    <template slot-scope="scope">
                        <span>
                            {{scope.row.card_number.slice(0,4)}}******{{ scope.row.card_number.slice(scope.row.card_number.length-4,scope.row.card_number.length) }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="身份证号" align="center" prop="idcard" />
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <span>
                            {{selectDictLabel(statusList, scope.row.status)}}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="title='编辑';form=JSON.parse(JSON.stringify(scope.row));handleShow='edit'"
                            v-hasPermi="['user:depart:edit']">
                            编辑
                        </el-button>
                        <!-- <el-button v-if="scope.row.status==0" size="mini" type="text" @click="handleAble(scope.row)" v-hasPermi="['user:depart:stop']">停用
                        </el-button>
                        <el-button v-if="scope.row.status==1" size="mini" type="text" @click="handleAble(scope.row)" v-hasPermi="['user:depart:start']">启用
                        </el-button> -->
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['user:depart:del']">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="pageCount>0" :pageCount="pageCount" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
        </div>
        <el-dialog :title="title" visible v-if="handleShow" @close="close()" width="600px" append-to-body :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-loading="formLoading">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="账户名称" prop="name">
                            <el-input v-model="form.name" placeholder="请输入账户名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="预留手机号" prop="mobile">
                            <el-input v-model="form.mobile" placeholder="预留手机号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="身份证号" prop="idcard">
                            <el-input v-model="form.idcard" placeholder="身份证号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="开户地" prop="open_location">
                            <el-input v-model="form.open_location" placeholder="开户地" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="开户支行" prop="open_bank">
                            <el-input v-model="form.open_bank" placeholder="开户支行" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="银行卡号" prop="card_number">
                            <el-input v-model="form.card_number" placeholder="银行卡号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="所属银行" prop="bank_id">
                            <el-select v-model="form.bank_id" placeholder="请选择所属银行" clearable size="small" filterable>
                                    <el-option v-for="dict in bankList" :key="dict.id" :label="dict.name" :value="dict.id" />
                                </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close()">取 消</el-button>
                <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { getAccountLists, setAccountInfo, getBanksLists, getAccountListsAll, delAccount  } from "@/api/finance/finance.js"
import { isvalidatemobile, cardid, isbankAccount } from '@/utils/validate'
import { departStatus } from "@/api/dictionary"

import throttle from 'lodash.throttle'
export default {
    name: 'account',
    props: {

    },
    data() {
        const ismobile = (rule, value, callback) => {
            if (isvalidatemobile(value)[0]) {
                callback(new Error(isvalidatemobile(value)[1]))
            } else {
                callback()
            }
        }
        const cardId = (rule, value, callback) => {
            if (cardid(value)[0]) {
                callback(new Error(cardid(value)[1]))
            } else {
                callback()
            }
        }
        const isBankAccount = (rule, value, callback) => {
            if (isbankAccount(value)[0]) {
                callback(new Error(isbankAccount(value)[1]))
            } else {
                callback()
            }
        }
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            queryParams: {
                page: 1,
                limit: 20,
                is_show_hide:1
                // key: null,
            },
            tableList: [],
            statusList: departStatus,
            handleShow: "",
            formLoading: false,
            form: {
                id: null,
                name: null,
            },
            rules: {
                name: [{ required: true, message: '请输入账户名称', trigger: 'blur' }],
                mobile: [{ required: true, message: '请输入预留手机号', trigger: 'blur' }, { validator: ismobile, trigger: 'blur' }],
                idcard: [{ required: true, message: '请输入身份证号', trigger: 'blur' }, { validator: cardId, trigger: 'blur' }],
                open_location: [{ required: true, message: '请输入开户地', trigger: 'blur' }],
                open_bank: [{ required: true, message: '请输入开户支行', trigger: 'blur' }],
                card_number: [{ required: true, message: '请输入银行卡号', trigger: 'blur' },{ validator: isBankAccount, trigger: 'blur' }],
                // { pattern: /^([1-9]{1})(\d{14}|\d{18})$/, message: '银行卡号格式不正确', trigger: 'blur' }
                bank_id: [{ required: true, message: '请选择所属银行', trigger: 'blur' }],
            },
            title: "",
            bankList:[],
        }
    },
    created() {
        this.getList()
        this.getBanksLists()
    },
    methods: {
        getList() {
            this.loading = true;
            getAccountLists(this.queryParams).then(res => {
                this.tableList = res.data.lists;
                this.pageCount = res.data.pageCount;
                this.loading = false;
            });
        },
        getBanksLists(){
            getBanksLists().then(res=>{
               this.bankList = res.data
            })

        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        close() {
            this.form = {}
            this.handleShow = ""
        },
        handleAble(row) {
            let label = row.status == 0 ? '停用' : '启用';
            this.$confirm(`是否将“${row.name}”${label}?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setSectorStatus({ sector_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess(`${label}成功`);
            })
        },
        handleDelete(row) {
            this.$confirm('是否确认删除' + '"' + row.name + '"' + '?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delAccount({ account_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },

        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    this.form.account_id = this.form.id ? this.form.id : null
                    setAccountInfo(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.getList();
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),
    }
}
</script>

<style lang='scss' scoped>
</style>
